package leetcode.offer_two_special.string;

/**
 * @author mazouri
 * @create 2021-09-06 20:35
 */
public class _20_countSubstrings {
    char[] chars;

    public int countSubstrings(String s) {
        if (s == null || s.length() == 0) return 0;
        chars = s.toCharArray();
        int count = 0;
        for (int i = 0; i < chars.length; i++) {
            //aba 处理奇数的情况
            count += countPalindrome(i, i);
            //abba 处理偶数的情况
            count += countPalindrome(i, i + 1);
        }
        return count;
    }

    private int countPalindrome(int start, int end) {
        int count = 0;
        while (start >= 0 && end < chars.length && chars[start] == chars[end]) {
            count++;
            //向两边扩散
            start--;
            end++;
        }
        return count;
    }
}
